package com.lzh.java;

import java.io.Serializable;

/**
 * @author liuzhihao
 * @create 2021-02-25 18:59
 */

/**
 *  Person需要满足如下的要求，方可序列化
 *  1.需要实现接口：Serializable
 *  2.需要当前类提供一个全局常量serialVersionUID
 *  3.除了当前Person类需要实现Serializable接口之外，还必须保证其内部所有属性也必须是可序列化的（默认情况下，基本数据类型可序列化）
 *
 * 4.序列化机制：
     * 对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流，从
     * 而允许把这种二进制流持久地保存在磁盘上，或通过网络将这种二进制流传
     * 输到另一个网络节点。//当其它程序获取了这种二进制流，就可以恢复成原
     * 来的Java对象
 * 补充：ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量
 */
public class Person implements Serializable {

    public static final long serialVersionUID = 45463312345L;

    private String name;
    private int age;
    private Account acct;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Person(String name, int age, Account acct) {
        this.name = name;
        this.age = age;
        this.acct = acct;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", acct=" + acct +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}
class Account implements  Serializable{

    public static final long serialVersionUID = 45463312345L;


    private double balance;

    public Account(double balance) {
        this.balance = balance;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "Account{" +
                "balance=" + balance +
                '}';
    }
}
